package iosr11.gbase.dao;

import iosr11.data.Mark;
import iosr11.data.Student;
import iosr11.data.Teacher;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;

public class MarkDAOImpl extends GenericDAOImpl<Mark> implements MarkDAO {

	/* nazwy pol do wyszukiwania */
	private static String TYPE = "Mark";
	private static String NAME_STUDENT_REF = "student";
	private static String NAME_TEACHER_REF = "teacher";

	private static String DATE = "date";
	private static String GRADE = "grade";
	private static String KIND = "kind";
	private static String POINTS = "points";
	private Logger log = Logger.getLogger("service-log");

	@Override
	public List<Mark> getStudentMarks(Student entity) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault()
				.getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance()
				.getGoogleBaseQueryResult(query, NAME_STUDENT_REF,
						entity.getId());
		if (feed == null) {
			log.info("brak rekordow w bazie majacych referencje do studenta: "+entity.getName());
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak ocen dla studenta: "+entity.getName());
			return null;
		}
		List<Mark> result = new ArrayList<Mark>();
		for (GoogleBaseEntry entry : feed.getEntries()) {
			Mark tmp = this.rewrite(entry);
			tmp.setStudent(entity);
			result.add(tmp);
		}
		log.info("znaleziono "+result.size()+" ocen nalezacych do studenta: "+entity.getName());
		return result;
	}

	@Override
	public Class<Mark> getEntityClass() {
		return Mark.class;
	}

	@Override
	public Mark rewrite(GoogleBaseEntry entry) {
		Mark tmp = new Mark();
		tmp.setDate(entry.getGoogleBaseAttributes().getDateTimeAttribute(
				DATE));
		tmp.setGrade(entry.getGoogleBaseAttributes().getFloatAttribute(
				GRADE));
		tmp.setId(entry.getId());
		tmp.setKind(tmp.changeIntToMarkKind(entry.getGoogleBaseAttributes()
				.getIntAttribute(KIND)));
		tmp.setPoints(entry.getGoogleBaseAttributes().getFloatAttribute(
				POINTS));
		Student student = new Student();
		student.setId(entry.getGoogleBaseAttributes()
				.getTextAttribute(NAME_STUDENT_REF));
		tmp.setStudent(student);
		Teacher teacher = new Teacher();
		teacher.setId(entry.getGoogleBaseAttributes()
				.getTextAttribute(NAME_TEACHER_REF));
		tmp.setTeacher(teacher);
		return tmp;
	}

	@Override
	public GoogleBaseEntry rewrite(Mark entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.setDraft(true);
		entry.getGoogleBaseAttributes().setItemType(TYPE);
		entry.getGoogleBaseAttributes().addDateTimeAttribute(DATE,
				entity.getGDate());
		entry.getGoogleBaseAttributes().addFloatAttribute(GRADE,
				entity.getGrade());
		entry.getGoogleBaseAttributes().addIntAttribute(KIND,
				entity.changeMarkKindToInt(entity.getKind()));
		entry.getGoogleBaseAttributes().addFloatAttribute(POINTS,
				entity.getPoints());

		// referencje na teachera i studenta
		entry.getGoogleBaseAttributes().addTextAttribute(NAME_STUDENT_REF,
				entity.getStudent().getId());
		entry.getGoogleBaseAttributes().addTextAttribute(NAME_TEACHER_REF,
				entity.getTeacher().getId());
		return entry;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

}
